<?php
namespace Admin\Controller;
//use Think\Controller;
class StaffController extends CommontController {
	//员工添加的完成
	public function add(){
		//实例化系别分类类模型
        $categoryModel=D('Category');
        //实现系别分类查询
        $data=$categoryModel->select();
        $data=getCatTree($data);
        $this->assign('data',$data);

        //实例化部门信息模型
        $DepartmentModel=D('Department');
        //实现部门信息查询
        $depart=$DepartmentModel->select();
        $this->assign('depart',$depart);

        //实例化学历信息模型
        $educationModel=D('Education');
        //实现部门信息查询
        $education=$educationModel->select();
        $this->assign('education',$education);

        //实现员工添加
        $staffModel=D("Staff");
        if(IS_POST){
        	if($staffModel->create()){
        		if($staffModel->add()){
        			$this->success("添加成功",U("Staff/index"));die;
        		}else{
        			$this->error("添加失败");
        		}
        	}else{
        		$this->error($staffModel->getError());
        	}
        }
        //渲染模板
        $this->display();
    }
    //员工列表的显示
    public function index(){
    	//实例化系别分类类模型
        $categoryModel=D('Category');
        //实现系别分类查询
        $data=$categoryModel->select(array('index'=>'cat_id'));
        $this->assign('data',$data);

         //实例化部门信息模型
        $DepartmentModel=D('Department');
        //实现部门信息查询
        $depart=$DepartmentModel->select(array('index'=>'de_id'));
        $this->assign('depart',$depart);

        //实例化学历信息模型
        $educationModel=D('Education');
        //实现部门信息查询
        $education=$educationModel->select(array('index'=>'edu_id'));
        $this->assign('education',$education);


    	//实例化员工类模型
    	//员工列表的显示完成
    	$staffModel=D("Staff");
        //分页操作
        $count=$staffModel->count();
        $Page=new \Think\Page($count,3);
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $pageList=$Page->show();
        $this->assign('pageList',$pageList);
    	$list=$staffModel->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
    	$this->assign('list',$list);
    	//渲染模板
    	$this->display();
    }
    //员工修改的显示
    public function upd(){
    	//系别分类显示
        $categoryModel=D('Category');
        $cat=$categoryModel->select();
        $cat=getCatTree($cat);
        $this->assign('cat',$cat);

        //实例化部门信息模型
        $DepartmentModel=D('Department');
        //实现部门信息查询
        $depart=$DepartmentModel->select();
        $this->assign('depart',$depart);

        //实例化学历信息模型
        $educationModel=D('Education');
        //实现部门信息查询
        $education=$educationModel->select();
        $this->assign('education',$education);

    	//员工列表回显
    	$staffModel=D("Staff");
     	$id=I('get.id');
     	$data=$staffModel->find($id);
        $this->assign('data',$data);
        //员工信息修改的实现
        if(IS_POST){
        	if($staffModel->create()){
        		if($staffModel->save()!==false){
        			$this->success("修改成功",U("Staff/index"));die;
        		}else{
        			$this->error("修改失败");
        		}
        	}else{
        		$this->error($staffModel->getError());
        	}
        }
    	//渲染模板
    	$this->display();
    }
    //员工修改的删除
    public function del(){
    	$staffModel=D("Staff");
    	$id=I('get.id');
     	$data=$staffModel->delete($id);
     	if($data){
     		$this->success("删除成功",U('Staff/index'));die;
     	}else{
     		$this->error("删除失败");
     	}
    }
}